package cn.iocoder.yudao.module.dr.controller.admin.collectionattachment;

import cn.iocoder.yudao.module.infra.api.file.FileApi;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dr.controller.admin.collectionattachment.vo.*;
import cn.iocoder.yudao.module.dr.dal.dataobject.collectionattachment.CollectionAttachmentDO;
import cn.iocoder.yudao.module.dr.service.collectionattachment.CollectionAttachmentService;

@Tag(name = "管理后台 - 藏品附件")
@RestController
@RequestMapping("/dr/collection-attachment")
@Validated
public class CollectionAttachmentController {

    @Resource
    private CollectionAttachmentService collectionAttachmentService;

    @Resource
    private FileApi fileApi;

    @PostMapping("/create")
    @Operation(summary = "创建藏品附件")
    //@PreAuthorize("@ss.hasPermission('dr:collection-attachment:create')")
    public CommonResult<Long> createCollectionAttachment(@Valid @RequestBody CollectionAttachmentSaveReqVO createReqVO) {
        return success(collectionAttachmentService.createCollectionAttachment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新藏品附件")
    //@PreAuthorize("@ss.hasPermission('dr:collection-attachment:update')")
    public CommonResult<Boolean> updateCollectionAttachment(@Valid @RequestBody CollectionAttachmentSaveReqVO updateReqVO) {
        collectionAttachmentService.updateCollectionAttachment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除藏品附件")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('dr:collection-attachment:delete')")
    public CommonResult<Boolean> deleteCollectionAttachment(@RequestParam("id") Long id) {
        collectionAttachmentService.deleteCollectionAttachment(id);
        fileApi.deleteFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得藏品附件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('dr:collection-attachment:query')")
    public CommonResult<CollectionAttachmentRespVO> getCollectionAttachment(@RequestParam("id") Long id) {
        CollectionAttachmentDO collectionAttachment = collectionAttachmentService.getCollectionAttachment(id);
        return success(BeanUtils.toBean(collectionAttachment, CollectionAttachmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得藏品附件分页")
    //@PreAuthorize("@ss.hasPermission('dr:collection-attachment:query')")
    public CommonResult<PageResult<CollectionAttachmentRespVO>> getCollectionAttachmentPage(@Valid CollectionAttachmentPageReqVO pageReqVO) {
        PageResult<CollectionAttachmentDO> pageResult = collectionAttachmentService.getCollectionAttachmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollectionAttachmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出藏品附件 Excel")
    //@PreAuthorize("@ss.hasPermission('dr:collection-attachment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectionAttachmentExcel(@Valid CollectionAttachmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollectionAttachmentDO> list = collectionAttachmentService.getCollectionAttachmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "藏品附件.xls", "数据", CollectionAttachmentRespVO.class,
                        BeanUtils.toBean(list, CollectionAttachmentRespVO.class));
    }

}